package com.atguigu.sparsearray;

import java.io.FileInputStream;

/**
 * 读取稀疏数组并转为二维数组实现
 * @author WangJX
 * @version 1.0
 * @date 2019/9/16 0:23
 */
public class ReadSparseArray {
    public static void main(String[] args) throws Exception {

        int sparseArr[][] = new int[3][3];

        //读取稀疏数组文件
        FileInputStream inputStream = new FileInputStream("E:\\new\\sparseArr.txt");
        System.out.println("读取到的稀疏数组为：");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                sparseArr[i][j] = inputStream.read();
                System.out.printf("%d\t", sparseArr[i][j]);
            }
            System.out.println();
        }


        //转化成的二维数组
        int chessArr[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i < sparseArr[0].length; i++) {
            chessArr[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        //打印二维数组
        System.out.println("转化成的二维数组为：");
        for (int[] ints : chessArr) {
            for (int anInt : ints) {
                System.out.printf("%d\t", anInt);
            }
            System.out.println();
        }
    }
}
